package whu.cn.whushare.ui.main.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.androidplus.util.StringUtil;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.UserManager;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.BaseActivity;
import whu.cn.whushare.ui.main.MainTabActivity;
import whu.cn.whushare.util.EncrypAES;

/**
 * Created by xiaoping on 2015/1/30.
 */
@ViewMapping(id=R.layout.activity_myinfo_nickname_change)
public class InfoNicknameChangeActivity extends BaseActivity {
    @ViewMapping(id = R.id.nick_name_change)
    private EditText mNickNameChange;
    private UserManager mUserManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initViewsAndActions();
    }
    public static Intent getStartIntent(Context context) {
        Intent i = new Intent(context, InfoNicknameChangeActivity.class);

        return i;
    }

    private void initViewsAndActions() {
        View root = ViewMappingUtil.mapView(this, this);
        getTopBar(root);
        mUserManager = UserManager.getInstance(this);
        mTopBar.title.setText(R.string.my_info_nick_name_change);
        mTopBar.rightText.setVisibility(View.VISIBLE);
        mTopBar.rightText.setText(R.string.save);
        initAction();
    }


    private void initAction() {
       mTopBar.rightText.setOnClickListener(this);


    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_right_txt:
                String nickName = mNickNameChange.getText().toString();


                if (validateNickName(nickName)) {
                    Downloader.getInstance(this).changeNickName(nickName,this);
                }
                break;
            default:
                break;
        }
    }
    private boolean validateNickName( String nickName) {

        msg = mUserManager.validateNickName(nickName).getMsg();
        if (!StringUtil.isNullOrEmpty(msg)) {
            toast(msg);
            return false;
        }
        return true;
    }
    public <T> void onSuccess(DownloadParams.PARSING_TYPE type, T response, Object obj) {
        super.onSuccess(type, response, obj);

        switch (type) {
            case CHANGE_NICKNAME:
                toast("修改成功！");
                startActivity(MyInfoActivity.getStartIntent(this));
                break;

            default:
                break;

        }

    }

}

